package 力扣日常刷题.木22二月.第15天221;

/**
 * @author 帅小伙
 * @date 2022/2/21
 * @description
 * 前缀和
 */
public class Demo116和为k的子数组 {

    public int subarraySum(int[] nums, int k) {
        int ans = 0;
        int n = nums.length;
        int[] sum = new int[n+1];
        for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nums[i - 1];

        for (int l = 0; l <= nums.length; l++) {
            for (int r = l+1; r <= nums.length; r++) {
                if((sum[r] - sum[l]) == k) ans++;
            }
        }

        return ans;
    }

}
